import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import situation from './situation'
import user from './user'
import order from './order'
import marketing from './marketing'
import analyse from './analyse'
import set from './set'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login',
    meta: { title: '登陆' }
  },
  {
    path: '/login',
    component: Login,
    meta: { title: '登陆' }
  },
  {
    path: '/home',
    component: Home,
    redirect: '/home/realTime',
    children: [
      //概况
      ...situation,
      //会员管理
      ...user,
      //订单管理
      ...order,
      //营销
      ...marketing,
      //数据分析
      ...analyse,
      //设置
      ...set
    ]
  },
]



const router = new VueRouter({
  routes
})

//设置路由守卫
router.beforeEach((to, form, next) => {
  document.title = '会员管理系统 | '+to.meta.title
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
